<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div slot="header" class="clearfix" v-if="subjects.id && subjects.name">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ subjects.id ? subjects.name : '' }}</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      <el-row>
        <el-col :span="18">
          <!-- 搜索框 -->
            <el-form :inline="true" :model="queryList" class="demo-form-inline">
              <el-form-item label="目录名称">
                <el-input v-model="queryList.directoryName" placeholder=""></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="queryList.state" placeholder="活动区域">
                  <el-option label="已禁用" :value= 0></el-option>
                  <el-option label="已启用" :value= 1></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button  plain size="small" @click="restInput">清除</el-button>
                <el-button type="primary" size="small" @click="getDirectorys">查询</el-button>
              </el-form-item>
            </el-form>
        </el-col>
        <!-- 新增学科  -->
        <el-col :span="6" >
          <el-row type="flex" justify="end">
          <el-button v-if="subjects.id && subjects.name" type="text" icon="el-icon-back" style=""  @click="$router.back()">返回学科</el-button>
          <el-button class="colright" size="small" type="success" icon="el-icon-edit" style="float: right" @click="addDialogVisible = true" >新增学科</el-button>
          </el-row>
        </el-col>
      </el-row>

      <!-- 消息文案 -->
      <el-alert :title="`数据一共 ${counts} 条`" :closable="false" type="info" show-icon style="margin-bottom: 16px"> </el-alert>

      <!-- 表格区域 -->
      <el-table :data="directoryList" style="width: 100%;" stripe >
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template v-slot="{row}">
            <span>{{parseTime(row.addDate )}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="{row}">
            {{ row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="editState(scope.row)">{{ scope.row.state === 1 ? '禁用' : '启用' }}</el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="editDirectorys(scope.row)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="removeDir(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-row type="flex" class="row-bg" justify="end" style="margin-top: 12px">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryList.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="queryList.pagesize"
            layout="total, prev, pager, next, sizes, jumper"
            :total="counts">
          </el-pagination>
        </el-row>
      </el-card>
    </div>

    <!-- 新增时弹出的对话框 -->
    <el-dialog title="新增目录" :visible.sync="addDialogVisible" width="30%" @close="btnAddCancel">
      <el-form :model="addForm" label-width="80px" :rules="rules" ref="addDirectorysForm">
        <el-form-item v-if="!subjects.id && !subjects.name" label="所属学科" prop="subjectID">
          <el-select  v-model="addForm.subjectID" placeholder="请选择活动区域" style="width: 100%;">
          <el-option v-for="item in simpleData" :key="item.value" :label= item.label :value= item.value></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="addForm.directoryName" autocomplete="off" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-row type="flex" justify="end">
          <el-button @click="btnAddCancel">取 消</el-button>
          <el-button type="primary" @click="addDirectory">确 定</el-button>
        </el-row>
      </div>
    </el-dialog>

    <!-- 修改时弹出的对话框 -->
    <el-dialog title="修改目录" :visible.sync="editDialogVisible" width="30%" @close="btnAddCancel">
      <el-form :model="editForm" label-width="80px" :rules="rules" ref="addDirectorysForm">
        <el-form-item v-if="!subjects.id && !subjects.name" label="所属学科" prop="subjectID">
          <el-select v-model="editForm.subjectID" placeholder="请选择活动区域" style="width: 100%;">
            <el-option v-for="item in simpleData" :key="item.value" :label= item.label :value= item.value></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="editForm.directoryName" autocomplete="off" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-row type="flex" justify="end">
          <el-button @click="btnAddCancel">取 消</el-button>
          <el-button type="primary" @click="editDirectory">确 定</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list as getDirectorys, changeState, remove, add, update } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
import { parseTime } from '@/utils'

export default {
  created () {
    this.getDirectorys()
    this.simple()
  },
  data () {
    return {
      // table列表数据
      directoryList: [],
      // 请求的参数
      queryList: {
        page: 1,
        pagesize: 10,
        directoryName: '',
        state: null
      },
      // 总条数
      counts: 0,
      // 控制增加对话框
      addDialogVisible: false,
      // 控制修改对话框
      editDialogVisible: false,
      // 新增表单
      addForm: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      },
      // 简单数据列表
      simpleData: {},
      // 修改表单
      editForm: {
        subjectID: '',
        directoryName: ''
      },
      infoId: ''
    }
  },

  // 获取地址栏数据
  computed: {
    subjects () {
      return this.$route.query || null
    }
  },

  // 监听地址栏数据
  watch: {
    '$route.query' () {
      this.getDirectorys()
    }
  },

  methods: {
    parseTime,
    // 获取目录列表
    async getDirectorys () {
      try {
        const { data } = await getDirectorys(this.queryList)
        this.directoryList = data.items
        this.counts = data.counts
        // console.log(data)
      } catch (e) {
        this.$message.error('请求失败!')
      }
    },

    // 改变每页显示条数
    handleSizeChange (val) {
      this.queryList.pagesize = val
      console.log(val)
      this.getDirectorys()
    },

    // 改变页码数
    handleCurrentChange (val) {
      this.queryList.page = val
      this.getDirectorys()
    },

    // 改变状态
    async editState (row) {
      // console.log(row)
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        // console.log(result)
        this.$message.success('操作成功!')
        row.state = row.state === 1 ? 0 : 1
      } catch (e) {
        this.$message.error('请求错误!')
      }
    },

    // 清除input
    restInput () {
      this.queryList.directoryName = ''
      this.queryList.state = null
      this.getDirectorys()
    },

    // 删除功能
    async removeDir (row) {
      const configResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(configResult)
      if (configResult !== 'confirm') {
        return this.$message('您取消了删除操作!')
      }
      try {
        await remove(row)
        this.$message.success('删除成功!')
        this.getDirectorys()
        if (this.directoryList.length === 1 && this.queryList.page > 1) { this.queryList.page -= 1 }
      } catch (e) {
        this.$message.error('删除失败!')
      }
    },

    // 获取简单列表
    async simple () {
      try {
        const { data } = await simple()
        this.simpleData = data
        console.log(data)
      } catch (e) {
        console.log(e)
      }
    },

    // 确认新增
    addDirectory () {
      this.$refs.addDirectorysForm.validate(async valid => {
        if (valid) { // 表单校验
          try {
            if (this.subjects.id && this.subjects.name) {
              await add({
                subjectID: +this.subjects.id,
                directoryName: this.addForm.directoryName
              })
              this.$message.success('新增目录成功!')
              this.getDirectorys()
              this.clearForm()
              this.addDialogVisible = false
            } else {
              await add({ subjectID: this.addForm.subjectID, directoryName: this.addForm.directoryName })
              this.$message.success('新增目录成功!')
              this.getDirectorys()
              this.clearForm()
              this.addDialogVisible = false
            }
          } catch (e) {
            this.$message.error('新增目录失败!')
            console.log(e)
          }
        }
      })
    },

    // 新增取消
    btnAddCancel () {
      this.addDialogVisible = false
      this.editDialogVisible = false
      this.clearForm()
    },

    // 清除表单
    clearForm () {
      this.$refs.addDirectorysForm.resetFields()
      this.addForm.subjectID = ''
      this.editForm.subjectID = ''
    },

    // 点击修改显示弹窗
    editDirectorys (row) {
      // console.log(row)
      this.infoId = row.id
      this.editForm.subjectID = row.subjectID
      this.editForm.directoryName = row.directoryName
      this.editDialogVisible = true
    },

    // 确认修改
    editDirectory () {
      this.$refs.addDirectorysForm.validate(async valid => {
        if (valid) {
          try {
            if (this.subjects.id && this.subjects.name) {
              await update({
                id: this.infoId,
                subjectID: +this.subjects.id,
                directoryName: this.editForm.directoryName
              })
              this.$message.success('修改目录成功!')
              this.getDirectorys()
              this.clearForm()
              this.editDialogVisible = false
            } else {
              await update({ id: this.infoId, subjectID: this.editForm.subjectID, directoryName: this.editForm.directoryName })
              this.$message.success('修改目录成功!')
              this.getDirectorys()
              this.clearForm()
              this.editDialogVisible = false
            }
          } catch (e) {
            this.$message.error('修改目录失败!')
            console.log(e)
          }
        }
      })
    }

  }
}
</script>

<style scoped></style>
